package com.tsinghua.server.original.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsinghua.server.common.Result;
import com.tsinghua.server.common.utils.ExcleUtil;
import com.tsinghua.server.original.entity.FxjcCtz;
import com.tsinghua.server.original.entity.FxjcCyz;
import com.tsinghua.server.original.entity.FxjcJrz;
import com.tsinghua.server.original.entity.FxjqCyzEntity;
import com.tsinghua.server.original.service.DataSetService;
import com.tsinghua.server.original.service.FxjcCtzService;
import com.tsinghua.server.original.service.FxjcCyzService;
import com.tsinghua.server.original.service.FxjcJrzService;
import com.tsinghua.server.train.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Mr.Eric
 * @description:
 * @date 2022/3/2 13:38
 */
@RestController
@RequestMapping(value = "/data-set/")
@Slf4j
public class DataSetController {

    @Resource
    private DataSetService dataSetService;

    @Resource
    private FxjcCyzService fxjcCyzService;

    @Resource
    private FxjcCtzService fxjcCtzService;

    @Resource
    private FxjcJrzService fxjcJrzService;

    /**
     * 产业债分页
     * @param pageSize
     * @param pageNo
     * @param zwzt
     * @return
     */
    @GetMapping(value = "cyz/page")
    public Result cyzPage(@RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                       @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                       @RequestParam(name = "zwzt", required = false) String zwzt) {

        QueryWrapper<FxjcCyz> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcCyz> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcCyzService.page(page,queryWrapper));
    }


    /**
     * 产业债
     * @param file
     * @return
     */
    @PostMapping(value = "cyz/import")
    public Result importFile(@RequestParam("file") MultipartFile file) {
        return Result.succ(dataSetService.importFile(file));
    }


    /**
     * 城投债原始数据
     * @return
     */
    @GetMapping(value = "ctz/page")
    public Result ctzPage(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt){
        QueryWrapper<FxjcCtz> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcCtz> page = new Page<>(pageNo , pageSize);
        fxjcCtzService.page(page,queryWrapper);
        return Result.succ(fxjcCtzService.page(page,queryWrapper));
    }


    @PostMapping(value = "ctz/import")
    public Result importCTZFile(@RequestParam("file") MultipartFile file) {
        return Result.succ(dataSetService.importCTZFile(file));
    }


    /**
     * 金融债
     * @param pageSize
     * @param pageNo
     * @param zwzt
     * @return
     */
    @GetMapping(value = "jrz/page")
    public Result jrzPage(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt){
        QueryWrapper<FxjcJrz> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcJrz> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcJrzService.page(page,queryWrapper));
    }


    @PostMapping(value = "jrz/import")
    public Result importJRZFile(@RequestParam("file") MultipartFile file) {
        return Result.succ(dataSetService.importJRZFile(file));
    }


    /**
     * 数据导出
     * @param dataType
     * @param response
     */
    @GetMapping("export")
    public void export(@RequestParam("dataType") Integer dataType, HttpServletResponse response){
        switch (dataType){
            case 1:
                List<FxjcCtz> fxjcCtz = fxjcCtzService.list();
                if (CollectionUtil.isNotEmpty(fxjcCtz)) {
                    ExcleUtil.exportExcel(fxjcCtz, null, "fxjcCtz", FxjcCtz.class, "fxjcCtz.xlsx", response);
                }
                break;
            case 2:
                List<FxjcCyz> fxjcCyz = fxjcCyzService.list();
                if (CollectionUtil.isNotEmpty(fxjcCyz)) {
                    ExcleUtil.exportExcel(fxjcCyz,null,"fxjcCyz",FxjcCyz.class,"fxjcCyz.xlsx", response);
                }
                break;
            case 3:
                List<FxjcJrz> fxjcJrz = fxjcJrzService.list();
                if (CollectionUtil.isNotEmpty(fxjcJrz)){
                    ExcleUtil.exportExcel(fxjcJrz,null,"fxjcJrz",FxjcJrz.class,"fxjcJrz.xlsx", response);
                }
                break;
        }
    }


    @PostMapping(value = "reimport")
    @Transactional
    public Result reImportExcle(@RequestParam("file") MultipartFile file,@RequestParam("dataType") Integer dataType){
        long start = System.currentTimeMillis();
        switch (dataType){
            case 1:
                List<FxjcCtz> fxjcCtz = ExcleUtil.importData(file,1,FxjcCtz.class);
                fxjcCtzService.delAll();
                fxjcCtzService.saveBatch(fxjcCtz);
                break;
            case 2:
                List<FxjcCyz> fxjcCyz = ExcleUtil.importData(file,1,FxjcCyz.class);
                fxjcCyzService.delAll();
                fxjcCyzService.saveBatch(fxjcCyz);
                break;
            case 3:
                List<FxjcJrz> fxjcJrz = ExcleUtil.importData(file,1,FxjcJrz.class);
                fxjcJrzService.delAll();
                fxjcJrzService.saveBatch(fxjcJrz);
                break;
        }
        long end = System.currentTimeMillis();
        log.info("耗时{}",end-start);
        return Result.succ(null);
    }

}
